package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.supervision.convert.AreaFenceConvert;
import com.engine4cloud.tdf.supervision.dto.query.AreaFenceQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.AreaFenceResultDTO;
import com.engine4cloud.tdf.supervision.entity.AreaFenceEntity;
import com.engine4cloud.tdf.supervision.mapper.AreaFenceMapper;
import com.engine4cloud.tdf.supervision.service.AreaFenceService;
import org.springframework.stereotype.Service;


/**
 * 行政区划
 *
 * @author wenjie
 * @date 2024-02-16 22:40:06
 */
@Service
public class AreaFenceServiceImpl extends ServiceImpl<AreaFenceMapper, AreaFenceEntity> implements AreaFenceService {

    @Override
    public PageInfo<AreaFenceResultDTO> pageList(Params pageable, AreaFenceQueryDTO dto) {
        IPage<AreaFenceResultDTO> pages = this.baseMapper.pageList(Pages.page(pageable), dto);
        return Pages.convert(pages);
    }

    @Override
    public AreaFenceResultDTO getByCode(String code) {
        return AreaFenceConvert.INSTANCE.convert(getOne(wrapperOne(new AreaFenceQueryDTO(code))));
    }

    private LambdaQueryWrapper<AreaFenceEntity> wrapperOne(AreaFenceQueryDTO query) {
        LambdaQueryWrapper<AreaFenceEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(AreaFenceEntity::getId, v));
        Queries.accept(query.getCode(), v -> wrapper.eq(AreaFenceEntity::getCode, v));
        return wrapper;
    }
}
